﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;

namespace eTerm.NetSocket
{
    public abstract class __TSession : BaseDisposable
    {
        #region 变量定义
        private SocketPacket m_inPakcet = new SocketPacket();
        private SocketPacket m_outPacket = new SocketPacket();
        #endregion

        #region 构造函数
        /// <summary>
        /// Initializes a new instance of the <see cref="__TSession"/> class.
        /// </summary>
        public __TSession()
        {
        }
        #endregion

        #region 属性定义
        /// <summary>
        /// 是否为SSL安全连接.
        /// </summary>
        /// <value><c>true</c> if this instance is SSL; otherwise, <c>false</c>.</value>
        public bool IsSsl { set; get; }

        /// <summary>
        /// 远程主机地址.
        /// </summary>
        /// <value>The remote ip.</value>
        public IPEndPoint RemoteEP { protected set; get; }

        /// <summary>
        /// 本地地址绑定.
        /// </summary>
        /// <value>The local EP.</value>
        public EndPoint LocalEP { get;protected set; }

        /// <summary>
        /// 发送区.
        /// </summary>
        /// <value>The datapacket.</value>
        public SocketPacket Inpacket { get { return m_inPakcet; } protected set { m_inPakcet = value; } }


        /// <summary>
        /// 接收区.
        /// </summary>
        /// <value>The datapacket.</value>
        public SocketPacket Outpacket { get { return m_outPacket; } protected set { m_outPacket = value; } }


        /// <summary>
        /// 远程主机地址
        /// </summary>
        public string Address { get; set; }

        /// <summary>
        /// 剩余指令数.
        /// </summary>
        /// <value>The surplus CMD count.</value>
        public long SurplusCmdCount { protected get; set; }

        /// <summary>
        /// 主机端口号.
        /// </summary>
        /// <value>The port.</value>
        public int Port { get; set; }

        /// <summary>
        /// 会话是否处理连接状态.
        /// </summary>
        /// <value><c>true</c> if connected; otherwise, <c>false</c>.</value>
        public bool Connected
        {
            get
            {
                return this.SyncSocket == null ? false : SyncSocket.Connected;
            }
        }

        /// <summary>
        /// 重连接次数.
        /// </summary>
        /// <value>The reconnect count.</value>
        public ushort ReconnectCount { get; set; }

        /// <summary>
        /// 上一次断开时间.
        /// </summary>
        /// <value>The last disconnect.</value>
        public DateTime? LastDisconnect { get; set; }

        /// <summary>
        /// 上一次连接时间.
        /// </summary>
        /// <value>The last connect.</value>
        public DateTime? LastConnect { get; set; }

        /// <summary>
        /// 上一次接收时间.
        /// </summary>
        /// <value>The last packet.</value>
        public DateTime? LastPacket { get; set; }

        /// <summary>
        /// 自建立链接以来总交互字节数(单位：KBytes).
        /// </summary>
        /// <value>The total bytes.</value>
        public decimal TotalBytes { get; set; }

        /// <summary>
        /// 最后接收字节数.
        /// </summary>
        /// <value>The current bytes.</value>
        public decimal CurrentBytes { get; set; }

        /// <summary>
        /// 建立连接以来总通信次数.
        /// </summary>
        /// <value>The total count.</value>
        public long TotalCount { get; set; }

        /// <summary>
        /// 会话端数据流对象.
        /// </summary>
        /// <value>The async stream.</value>
        public Stream SyncNetworkStream { get; set; }


        /// <summary>
        /// 会话端句柄号.
        /// </summary>
        /// <value>The session id.</value>
        public int SessionId { get; set; }

        /// <summary>
        /// 会话端Socket有效连接.
        /// </summary>
        /// <value>The sync socket.</value>
        public Socket SyncSocket { get; set; }
        #endregion

        #region 基类重写
        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return string.Format(@"{0}:{1} {2}", Address, Port,SessionId);
        }

        /// <summary>
        /// 非密封类修饰用protected virtual
        /// 密封类修饰用private
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            if (disposing) {
                try
                {
                    if (SyncSocket.Connected)
                    { SyncSocket.Shutdown(SocketShutdown.Both); }

                    SyncNetworkStream.Dispose();
                    SyncNetworkStream = null;
                    SyncSocket.Close();
                    SyncSocket = null;
                }
                catch { }
            }
        }
        #endregion
    }
}
